package com.rz.paas.baselib.netRequest

import android.content.Context
import com.rz.paas.baselib.netRequest.request.IHttpRequest
import com.rz.paas.baselib.netRequest.request.okhttp.OkHttpRequest
import com.rz.paas.baselib.netRequest.response.JsonCallback


object HttpClient {
    private var mHttpRequest: IHttpRequest? = OkHttpRequest ()
    var mContext: Context? = null

    fun with(context: Context): HttpClient {
        mContext = context;
        return this
    }

    fun httpRequest(httpRequest: IHttpRequest): HttpClient {
        mHttpRequest = httpRequest
        return this
    }

    private val mParams = hashMapOf<String, Any?>()
    private var mUrl: String? = null
    private var mCache: Boolean = false
    // 添加参数
    fun addParam(key: String, value: Any?): HttpClient {
        mParams.put(key, value)
        return this
    }

    fun addParams(params: Map<String, Any?>): HttpClient {
        mParams.putAll(params)
        return this
    }

    fun url(url: String): HttpClient {
        mUrl = url
        return this
    }

    fun cache(cache: Boolean): HttpClient {
        mCache = cache
        return this
    }
    fun <T> execute(callback: JsonCallback<T>) {

        mHttpRequest!!.get(mContext, mParams as Map<String, Any?>, mUrl, mCache, callback)
        mHttpRequest!!.post(mContext, mParams as Map<String, Any?>, mUrl, mCache, callback)
    }
}